import { defineStore } from "pinia";
import { RouteRecordRaw } from 'vue-router'
import { asyncRoutes } from '@/router'


// export const filterAsyncRoutes = (routes: RouteRecordRaw[]) => {
//     const res: RouteRecordRaw[] = []
//     for (const route of routes) {
//         // 判断 route 是否符合你的条件，如果是，就添加到 res 数组中
//         if (route.children) {
//             // 这里只是一个示例，你可以根据你自己的逻辑来判断
//             route.children = filterAsyncRoutes(route.children)
//         }
//         res.push(route)
//     }
//     return res
// }
export const filterAsyncRoutes = (routes: RouteRecordRaw[]) => {
    const res: RouteRecordRaw[] = []
    routes.forEach(route => {
      const r = { ...route }
        if (r.children) {
          r.children = filterAsyncRoutes(r.children)
        }
        res.push(r)
    })
    // console.log("res",res);
    
    return res
}


export const usepermissionStore = defineStore("permission", {
    state: () => {
        return {
            dynamicRoutes: [] as RouteRecordRaw[],
            addRouteState: false
        }
    },
    getters: {

    },
    actions: {
        SET_ROUTES(routes: RouteRecordRaw[]){
            this.dynamicRoutes = routes
        },
        // 生成动态路由
        generateRoutes() {
            let accessedRoutes: RouteRecordRaw[]
            accessedRoutes = filterAsyncRoutes(asyncRoutes)
            this.SET_ROUTES(accessedRoutes)
        },
        AddRouteState(addState: boolean) {
            // console.log('addState', addState);
            this.addRouteState = addState
        },
        SetAddRoutesState(stateValue: boolean){
            this.AddRouteState(stateValue)
        }

    },
    // persist:{
    //     enabled:true
    //   }
});